package com.vxb.testmvp.materialdesign;

import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.vxb.testmvp.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import de.hdodenhof.circleimageview.CircleImageView;

public class MaterialActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    private Partner[] partners = {
            new Partner("路飞",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("索隆",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("山治",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("艾斯",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("罗",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("娜美",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("罗宾",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("薇薇",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("蕾贝卡",R.mipmap.ic_launcher,R.string.app_name),
            new Partner("汉库克",R.mipmap.ic_launcher,R.string.app_name)};

    private List<Partner> partnerList = new ArrayList<>();

    private PartnerAdapter adapter;
    private SwipeRefreshLayout swipeRefresh;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      /*  ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true); //让导航按钮显示出来
            actionBar.setHomeAsUpIndicator(andr);//设置导航按钮图标
        }*/
        /*
        * 那个按钮 旋转, 显示或者隐藏.
        * */

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                toolbar,
                R.string.app_name,
                R.string.app_name);
        mDrawerLayout.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
        navView.setCheckedItem(R.id.nav_call);//设置默认选中项
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();//关闭抽屉
                Toast.makeText(MaterialActivity.this, "点击了" + item.getTitle(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        View headerView = navView.getHeaderView(0);
        CircleImageView headImg = (CircleImageView) headerView.findViewById(R.id.icon_image);
        headImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MaterialActivity.this, "点击了头像", Toast.LENGTH_SHORT).show();
            }
        });


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MaterialActivity.this, "点击了悬浮按钮", Toast.LENGTH_SHORT).show();
                Snackbar.make(v, "删除数据", Snackbar.LENGTH_SHORT)
                        .setAction("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MaterialActivity.this, "数据恢复", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });


        initPartner();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_one_piece);
        GridLayoutManager layoutManager = new GridLayoutManager(this,2);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new PartnerAdapter(partnerList);
        recyclerView.setAdapter(adapter);


        // 下拉刷新
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);//设置刷新进度条颜色
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 处理刷新逻辑
                refreshPartner();
            }
        });


    }


    /**
     * 下拉刷新数据（为简单起见没和网络交互）
     */
    private void refreshPartner() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initPartner();//重新生成数据
                        adapter.notifyDataSetChanged();//通知数据变化
                        swipeRefresh.setRefreshing(false);//隐藏刷新进度条
                    }
                });
            }
        }).start();
    }

    /**
     * 初始化数据，随机挑选50条数据
     */
    private void initPartner() {
        partnerList.clear();
        for (int i = 0;i < 50 ;i++){
            Random random = new Random();
            int index = random.nextInt(partners.length);
            partnerList.add(partners[index]);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 加载菜单
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 设置点击事件
        switch (item.getItemId()) {
            case R.id.backup:
                break;
            case R.id.delete:
                break;
            case R.id.settings:
                break;
        }
        return true;
    }

}
